﻿using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows;

namespace com.orange.examination.Student
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["ID"] == null)
            {
                Response.Redirect("../LoginAndRegister/Login.aspx");
            }
            else
            if (!IsPostBack)
            {
                lblNum.Text = Session["ID"].ToString();
                MySqlConnection conn = BaseClass.DBCon();
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("select * from tb_Student where StudentNum='" + Session["ID"].ToString() + "'", conn);
                MySqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                lblName.Text = sdr["StudentName"].ToString();
                lblsex.Text = sdr["StudentSex"].ToString();
                conn.Close();
                Session["name"] = lblName.Text;
                Session["sex"] = lblsex.Text;
                BindDropDownList();
            }
        }
        private void BindDropDownList()
        {
            MySqlConnection conn = BaseClass.DBCon();
            conn.Open();
            MySqlCommand cmd = new MySqlCommand("select LessonName from tb_Lesson", conn);
            MySqlDataReader sdr = cmd.ExecuteReader();
            ddlKm.DataSource = sdr;
            ddlKm.DataTextField = "LessonName";
            ddlKm.DataBind();
            conn.Close();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (ckbCheck.Checked == true)
            {
                string StuID = Session["ID"].ToString();//考生的编号
                string StuKC = ddlKm.SelectedItem.Text;//选择的考试科目
                MySqlConnection conn = BaseClass.DBCon();
                conn.Open();
                MySqlCommand cmd = new MySqlCommand("select count(*) from tb_Score where StudentID='" + StuID + "' and LessonName='" + StuKC + "'", conn);
                int i = Convert.ToInt32(cmd.ExecuteScalar());
                if (i > 0)
                {
                    Response.Write("<script>window.alert('你已经参加过此科目的考试了！');</script>");
                }
                else
                {

                    cmd = new MySqlCommand("select count(*) from tb_test where testCourse='" + StuKC + "'", conn);
                    int N = Convert.ToInt32(cmd.ExecuteScalar());

                    if (N > 0)
                    {
                        cmd = new MySqlCommand("insert into tb_Score(StudentID,LessonName,StudentName) values('" + StuID + "','" + StuKC + "','" + lblName.Text.ToString() + "')", conn);
                        cmd.ExecuteNonQuery();

                        conn.Close();
                        Session["KM"] = StuKC;
                        Session["falg"] = 1;
                        Response.Write("<script>window.open('StartExam.aspx','newwindow','status=1,scrollbars=1,resizable=1')</script>");
                        Response.Write("<script>window.opener=null;window.close();</script>");
                    }
                    else
                    {
                        Response.Write("<script>window.alert('此科目没有考试题！');</script>");
                        return;
                    }
                }
            }
            else
            {
                Response.Write("<script>window.alert('请接受考试规则！');</script>");
            }
        }
     
    }
}